using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace BizTalkCrossReferenceManager.UI.Generation.Generators
{
    /// <summary>
    /// Generates the xref data file
    /// </summary>
    public class BtsIdXRefDataGenerator
    {
        private const string FileName = "List_Of_IdXRef_Data.xml";
	
        /// <summary>
        /// Generates the file
        /// </summary>
        /// <param name="outputFolder"></param>
        public static void Generate(string outputFolder)
        {
            const string Start = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<listOfIDXRefData xmlns=\"http://www.microsoft.com/uan\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.microsoft.com/uan ../../CO/common.xsd\">";
            const string End = "</listOfIDXRefData>";

            string path = outputFolder + @"\" + FileName;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(Start);

            CrossReferenceDataDataSet.xref_IDXRefDataTable idTable = new CrossReferenceDataDataSet.xref_IDXRefDataTable();
            CrossReferenceDataDataSetTableAdapters.xref_IDXRefTableAdapter idAdapter = new CrossReferenceDataDataSetTableAdapters.xref_IDXRefTableAdapter();
            idAdapter.Fill(idTable);

            foreach (CrossReferenceDataDataSet.xref_IDXRefRow idRow in idTable.Rows)
            {                
                sb.AppendFormat("\t<idXRef name=\"{0}\">", idRow.idXRef);
                sb.AppendLine();

                //Get App Instances
                CrossReferenceDataDataSetTableAdapters.xref_AppInstanceTableAdapter appInstanceAdapter = new CrossReferenceDataDataSetTableAdapters.xref_AppInstanceTableAdapter();
                CrossReferenceDataDataSet.xref_AppInstanceDataTable appInstanceTable = appInstanceAdapter.GetData();   
                foreach(CrossReferenceDataDataSet.xref_AppInstanceRow appInstanceRow in appInstanceTable.Rows)
                {
                    CrossReferenceDataDataSetTableAdapters.xref_IDXRefDataViewTableAdapter xRefDataAdapter = new CrossReferenceDataDataSetTableAdapters.xref_IDXRefDataViewTableAdapter();
                    CrossReferenceDataDataSet.xref_IDXRefDataViewDataTable xRefDataTable = xRefDataAdapter.GetDataByAppInstanceIdAndXRefId(idRow.idXRefID, appInstanceRow.appInstanceID);
                    if (xRefDataTable.Rows.Count > 0)
                    {
                        sb.AppendFormat("\t\t<appInstance name=\"{0}\">", appInstanceRow.appInstance);
                        sb.AppendLine();

                        foreach (CrossReferenceDataDataSet.xref_IDXRefDataViewRow xRefDataRow in xRefDataTable.Rows)
                        {
                            sb.AppendFormat("\t\t\t<appID commonID=\"{0}\">{1}</appID>", xRefDataRow.commonIDDescription, xRefDataRow.appID);
                            sb.AppendLine();
                        }

                        sb.AppendLine("\t\t</appInstance>");
                    }
                }
                                		
                sb.AppendLine("\t</idXRef>");
            }

            sb.AppendLine(End);

            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(fs))
                {
                    writer.Write(sb.ToString());
                    writer.Flush();
                }
            }
        }
    }
}
